package org.dromara.common.core.exception

import cn.hutool.core.text.StrFormatter
import java.io.Serial

/**
 * 业务异常（支持占位符 {} ）
 *
 * @author ruoyi
 * @updater LikeYouDo
 * @date 2025/1/13 10:57
 */
data class ServiceException(
    /**
     * 错误提示
     */
    override var message: String? = null,

    /**
     * 错误码
     */
    var code: Int? = null,
    /**
     * 错误明细，内部调试错误
     */
    var detailMessage: String? = null
) : RuntimeException() {

    companion object {
        @Serial
        private val serialVersionUID: Long = 1L
    }

    constructor(message: String?) : this(message, null)
    constructor(message: String?, code: Int?) : this(message, code, null)
    constructor(message: String?, vararg args: Any?): this(StrFormatter.format(message, *args), null)

    fun setMessage(message: String?): ServiceException {
        this.message = message
        return this
    }

    fun setDetailMessage(detailMessage: String?): ServiceException {
        this.detailMessage = detailMessage
        return this
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is ServiceException) return false
        if (!super.equals(other)) return false

        if (message != other.message) return false
        if (code != other.code) return false
        if (detailMessage != other.detailMessage) return false

        return true
    }

    override fun hashCode(): Int {
        var result = super.hashCode()
        result = 31 * result + (message?.hashCode() ?: 0)
        result = 31 * result + (code ?: 0)
        result = 31 * result + (detailMessage?.hashCode() ?: 0)
        return result
    }


}
